package com.crayfish.quduo.presenter;

import com.crayfish.quduo.base.RxPresenter;
import com.crayfish.quduo.model.JokeItemBean;
import com.crayfish.quduo.presenter.contract.HotManagerContract;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 作者：crayfish(徐杰)
 * 描述：
 * Created by crayfish on 2017/3/29.
 */
public class HotManagerPresenter extends RxPresenter<HotManagerContract.View> implements HotManagerContract.Presenter{

    public HotManagerPresenter(){

    }

    @Override
    public void queryHot(final JokeItemBean.Item jItem) {
        BmobQuery<JokeItemBean.Item> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("img",jItem.getImg());
        bmobQuery.findObjects(new FindListener<JokeItemBean.Item>() {
            @Override
            public void done(List<JokeItemBean.Item> list, BmobException e) {
                if(e == null){
                    if(list.size() > 0){
                        mView.searchSuccess(list.get(0));
                    }else {
                        mView.searchSuccess(jItem);
                    }
                }
            }
        });
    }

    @Override
    public void updateHot(JokeItemBean.Item jItem) {
        jItem.setComment(jItem.getComment()+1);
        jItem.update(jItem.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {

            }
        });
    }

    @Override
    public void addHot(JokeItemBean.Item jItem) {
        jItem.setComment(1001);
        jItem.setLaud(500);
        jItem.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {

            }
        });
    }
}
